#region Copyright
/// <copyright>
/// Copyright (c) 2007 Ilia Shramko.
/// Released under the terms of the GNU General Public License.
/// </copyright>
#endregion

using System;
using System.Collections.Generic;
using System.Text;

using Osh.Teletext;

namespace Osh.Subtitles
{
	public class TeletextSubtitleParser : ISubtitleSource
	{
		private TeletextService _service;
		private PageAddress _pageAddress;

		public TeletextSubtitleParser(TeletextService service, PageAddress pageAddress)
		{
			_service = service;
			_pageAddress = pageAddress;

			_service.MagazineAdd += new EventHandler<TeletextEventArgs>(TeletextService_MagazineAdd);
		}

		private void TeletextService_MagazineAdd(object sender, TeletextEventArgs e)
		{
			if(e.Magazine.Number == _pageAddress.Magazine)
				e.Magazine.PageLoaded += new EventHandler<TeletextEventArgs>(Magazine_PageLoaded);
		}

		private void Magazine_PageLoaded(object sender, TeletextEventArgs e)
		{
			if(e.Page.Address != _pageAddress)
				return;

			string html = e.Page.GetPageHtml();
			if(this.SubtitlesReady != null)
				this.SubtitlesReady.Invoke(this, new SubtitlesEventArgs(html, TimeSpan.MinValue));
		}

		#region ISubtitleSource Members
		public bool IsRealTime
		{
			get { return true; }
		}

		public event EventHandler<SubtitlesEventArgs> SubtitlesReady;
		#endregion ISubtitleSource Members
	}

}
